var app = getApp();
var api = require("../../utils/api.js");
var util= require("../../utils/libs.js");
Page({
  data: {
    uid:"",
    title:'',
    description:'',
    images:'',
    select:'',
    text_number:300,
    is_draft:0
  },
  onLoad (options) {
    let that = this
    let uid = wx.getStorageSync("uid")
    that.setData({uid:uid})
    // 页面初始化 options为页面跳转所带来的参数
  },
  geText(event) {
    let that = this
    that.setData({
      select: {
        content: [300, 500, 700, 900, 1000,3000,'不限']
      }
    })
  },
  getSave() {
   let that = this
   that.setData({
    is_draft:1
   })
   if(that.data.title==''){
     wx.showToast({
       title: '请输入标题',
       icon:"none",
       duration:1000
     })
     return false;
   }
   if(that.data.content==''){
     wx.showToast({
       title: '请输入文章内容',
       icon:"none",
       duration:1000
     })
     return false;
   }
   util.request(api.topicAdd,{
    uid:that.data.uid,
    title:that.data.title,
    description:that.data.description,
    images:that.data.images,
    text_number:that.data.text_number,
    is_draft:that.data.is_draft
  },
  "POST")
  .then(function (res) {
    if(res.error_code === 0){
      let id = res.data.id
      wx.showToast({
        title: '保存成功',
        icon: 'success',
        duration: 2000,
        success:function(){
          wx.navigateTo({
            url:"/pages/writing/writing?id="+id
          })
        }
      })
    }
  })
  },
  getValue(event){
    let that = this
    let values = event.detail.value;
    that.setData({
      title: values
    })
   },
  selectV(event) {
    var val = event.currentTarget.dataset.val
      this.setData({
        text_number: val,
        select: ''
      })
  },
  closeS() {
    this.setData({
      select: ''
    })
  },
   getdescription(event){
    let that = this
    let values = event.detail.value;
    that.setData({
      description: values
    })
   },
  sendImgs () {
    let that = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          image: res.tempFilePaths
        }) 
      }
    })
  },
  closeImages(event){
    this.setData({
      images:''
    })
  },
  chooseImageTap (){
    let _this = this;
    wx.showActionSheet({
     itemList: ['从相册中选择', '拍照'],
     itemColor: "#f7982a",
     success: function(res) {
     if (!res.cancel) {
     if(res.tapIndex == 0){
     _this.chooseWxImage('album')
     }else if(res.tapIndex == 1){
     _this.chooseWxImage('camera')
     }
     }
     },
     fail: function(res) {
      console.log(res.errMsg)
    }
    })
},
chooseWxImage:function(type){
  let _this = this;
  wx.chooseImage({
   sizeType: ['original', 'compressed'],
   sourceType: [type],
   success: function (res) {
   _this.setData({
   logo: res.tempFilePaths[0],
   })
   app.upload_file("https://bucuo.jikeyun.net/api/upload",
    _this.data.logo,"upload",{},function success(res){
        res = JSON.parse(res)
      if(res.error_code === 0){
        _this.setData({
          images:res.data.file
        })
      }
    })
 }
})
},
addwrting(){
    let that = this
    if (that.data.title == '') {
      wx.showToast({
        title: '请输入标题',
        icon: "none",
        duration: 1000
      })
      return false;
    }
    if (that.data.content == '') {
      wx.showToast({
        title: '请输入文章内容',
        icon: "none",
        duration: 1000
      })
      return false;
    }
    util.request(api.topicAdd,{
      uid:that.data.uid,
      title:that.data.title,
      description:that.data.description,
      images:that.data.images,
      text_number:that.data.text_number,
      is_draft:that.data.is_draft
    },
    "POST")
    .then(function (res) {
      if(res.error_code === 0){
        let id = res.data.id
        wx.showToast({
          title: '发布成功',
          icon: 'success',
          duration: 2000,
          success:function(){
            wx.navigateTo({
              url:"/pages/writing/writing?id="+id
            })
          }
        })
      }
    })
  },
  onReady () {
    // 页面渲染完成
  },
  onShow () {
    // 页面显示
  },
  onHide () {
    // 页面隐藏

  },
  onUnload () {
    // 页面关闭

  }
})
